﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FtpTestTool
{

    /// <summary>
    /// 资源使用帮助助手
    /// </summary>
    public class ResourceHelper
    {

        #region ReleasComObject:释放COM对象
        /// <summary>
        /// 释放COM对象
        /// </summary>
        /// <param name="obj">要释放内存的集合COM组件对象</param>
        public static void ReleasComObject(object obj)
        {
            try
            {
                if (obj == null) return;

                var iRelease = System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                while (iRelease > 0)
                {
                    iRelease = System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                }
            }
            catch (Exception e)
            {
            }
            finally
            {
                if (obj != null) obj = null;
            }

        }
        /// <summary>
        /// 释放COM对象
        /// </summary>
        /// <param name="obj">要释放内存的集合COM组件对象</param>
        public static void ReleasComObjects(IList<object> obj)
        {
            if (obj == null) return;
            foreach (var geometry in obj)
            {
                ReleasComObject(geometry);
            }
        }
        #endregion

        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="filepaths"></param>
        public static void DeleFiles(List<string> filepaths)
        {
            filepaths.ForEach(c => DeleFiles(c));
        }



        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="filepath"></param>
        public static void DeleFiles(string filepath)
        {
            try
            {
                if (!File.Exists(filepath))
                    return;
                if (File.GetAttributes(filepath).ToString().IndexOf("ReadOnly") != -1)
                {
                    File.SetAttributes(filepath, FileAttributes.Normal);
                }
                File.Delete(filepath);
            }
            catch (Exception ex)
            {

            }
        }
    }
}
